<?php

use Illuminate\Database\Seeder;
use App\Http\Models\Admin\Permission_;
use App\Http\Models\Admin\PermissionsDetails;

class PermissionsDetailsSeed extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        // if (!DB::table('PermissionsDetails')->where('id', 1)->first()) {
        //     DB::table('ClassifyPermissions')->insert(
        //         ['id' => 1, 'title' => '用户管理','parent_id'=>'0', 'rank' => '10', 'created_at' => date('Y-m-d H:i:s'),]
        //     );
        // }
        $details = array(
            array('explain' => '修改权限', 'name' => 'update power'),
            array('explain' => '创建权限', 'name' => 'create power'),
            array('explain' => '查看权限', 'name' => 'watch power'),
            array('explain' => '删除权限', 'name' => 'delete power'),

            array('explain' => '创建用户', 'name' => 'create user'),
            array('explain' => '修改用户', 'name' => 'update user'),
            array('explain' => '查看用户', 'name' => 'watch user'),
            array('explain' => '删除用户', 'name' => 'delete user'),
            array('explain' => '修改密码', 'name' => 'change_password'),

            array('explain' => '创建角色', 'name' => 'create roles'),
            array('explain' => '修改角色', 'name' => 'update roles'),
            array('explain' => '查看角色', 'name' => 'watch roles'),
            array('explain' => '删除角色', 'name' => 'delete roles'),
        );

        foreach ($details as $key => $value) {
            $permission = Permission_::where(array('name' => $value['name']))->first();
            
            if ($permission) {
                // $details = new PermissionsDetails;
                // $details->permission_id = $permission->id;

                // $permission = $details->where(array('permission_id' => $permission->id))->get();
                // $details->explain = $value['explain'];

                // if (empty(count($permission))) {
                //     $details->save();
                // } else {
                //     $details->update();
                // }

                if (!DB::table('permissions_details')->where('permission_id', $permission->id)->first()) {
                    DB::table('permissions_details')->insert(
                        ['id'=>($key +1),'explain' => $value['explain'], 'permission_id' => $permission->id, 'rank' => '10']
                    );
                }
            }
        }
    }
}
